package safeCommitter.test.executor.launcher.listener;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.debug.core.ILaunch;
import org.tigris.subversion.subclipse.ui.operations.CommitOperation;

import safeCommitter.core.launcher.socket.SCSocketReader;
import safeCommitter.core.listener.SCListener;
import safeCommitter.core.template.Activator;

public class SCCommitLaunchListener extends SCListener {

	private final SCSocketReader reader;
	private final CommitOperation commit;
	private Boolean done = Boolean.FALSE;

	public SCCommitLaunchListener(final SCSocketReader reader,
			final CommitOperation c) {
		this.reader = reader;
		this.commit = c;
	}

	@Override
	public void launchesTerminated(final ILaunch[] launches) {
		while (!reader.getJobDone())
			;
		if (done) {
			return;
		}
		Activator.getDefault().fireTestsFinished();
		System.out.println("continue...");
		if (reader.getTestsPassing()) {
			try {
				if (commit != null) {
					commit.run();
				}
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				done = true;
			}
		}

	}
}
